/**
 * 
 */
package com.shine.icis.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author zsy
 *
 */
public class ZipHelper {
	/**
	 * 压缩文件列表
	 * @param files
	 * @param zipPath
	 */
	public static void compressFiles(List<File> files, String zipPath) {
		try {
			byte[] buffer = new byte[102400];
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
			for (File file : files) {
				if (!file.isFile()) {
					continue;
				}
				FileInputStream fis = new FileInputStream(file); 
				out.putNextEntry(new ZipEntry(file.getName()));
				int len;  
				while((len = fis.read(buffer)) > 0) {  
					out.write(buffer, 0, len);   
				}  
				out.closeEntry();  
				fis.close();  
			}
			out.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 压缩文件夹
	 * @param filePaths
	 * @param zipPath
	 */
	public static void compressDir(String dirPath, String zipPath) {
		File dir = new File(dirPath);
		List<File> filePaths = new ArrayList<File>();
		if (dir.isDirectory()) {
			for (File f : dir.listFiles()) {
				filePaths.add(f);
			}
		}
		
		compressFiles(filePaths, zipPath);
	}
}
